package com.spring.cootask.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spring.cootask.web.dao.CooTaskUserTableMapper;
import com.spring.cootask.web.model.CooTaskUserTableWithBLOBs;
import com.spring.cootask.web.service.CooTaskUserTableService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.BaseResult;
import com.spring.tools.XmlUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by WoJunyu on 2016/9/6.
 */
@Service
public class CooTaskUserTableServiceImpl implements CooTaskUserTableService{

    @Resource
    private CooTaskUserTableMapper cooTaskUserTableMapper;

    @Override
    public CooTaskUserTableWithBLOBs selectByPrimaryKey(String id) {
        return cooTaskUserTableMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseResult saveFormData(ShopOaSessionUtil.OaUser oaUser, List<Map<String, Object>> jsonData)
     throws Exception{
        BaseResult ret = new BaseResult();
        if(oaUser==null||!ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
            ret.setStatus("0");
            ret.setMsg("请先登录");
            return ret;
        }
        String jsonStr = "";
        String xmlStr = "";
        if(jsonData!=null&&jsonData.size()>0) {
            Map<String, Object> jsonM = jsonData.get(0);
            String id = ApplicationUtils.Object2String(jsonM.get("taskUserId"));
            if (ApplicationUtils.isNotNull(id)) {
                //解析成json
                jsonStr = JSON.toJSONString(jsonData);
                //解析成xml
                xmlStr = XmlUtils.list2xmlBody(jsonData, "root");
               // xmlStr = XmlUtils.map2xmlBody(jsonData.get(0), "root");
                CooTaskUserTableWithBLOBs model = new CooTaskUserTableWithBLOBs();
                model.setId(id);
                model.setChangedtime(new Date());
                model.setOperatorid(oaUser.getOa_user_id());
                model.setOperatorname(oaUser.getOa_user_name());
                model.setJsonData(jsonStr);
                model.setXmlData(xmlStr);
                cooTaskUserTableMapper.updateByPrimaryKeySelective(model);
            }else{
                ret.setStatus("0");
                ret.setMsg("主键不可为空");
            }
        }else{
            ret.setStatus("0");
            ret.setMsg("内容不可为空");
        }
        return ret;
    }
}
